package view;

import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.BufferedImage;
import java.util.logging.Level;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import model.Player;
import model.Preference;
import util.IView;
import util.LoggingUtils;
import util.PlayerUtilities;

/**
 */
public class PlayerCanvas extends JFrame implements WindowListener, MouseListener, IView {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * Field player.
	 */
	private Player player;
	/**
	 * Field layout.
	 */
	private PageLayout layout;

	//RBB
	protected final String className = this.getClass().getName();

	/**
	 * Constructor for PlayerCanvas.
	 * 
	 * @param player
	 *            Player
	 */
	public PlayerCanvas(Player player) {
		super("DMBPLAYER");
		this.player = player;
		this.initialize();
	}

	/**
	 * Method initialize.
	 */
	private void initialize() {
		Preference preference = Preference.getInstance();
		// this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		preference.addView(this);
		this.setSize(new Dimension(PlayerUtilities.scale(preference.getPlayerWidth(), preference.getScale()), PlayerUtilities.scale(
				preference.getPlayerHeight(), preference.getScale())));
		this.setLocation(0, 0);
		this.layout = new PageLayout(this.player);
		this.setContentPane(this.layout);
		this.setUndecorated(true);
		this.setAlwaysOnTop(true);
		this.addWindowListener(this);
		this.addMouseListener(this);
		Cursor invisibleCursor = Toolkit.getDefaultToolkit().createCustomCursor(new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB_PRE),
				new Point(0, 0), "invisibile");
		this.setCursor(invisibleCursor);
	}

	/**
	 * Method windowActivated.
	 * 
	 * @param e
	 *            WindowEvent
	 * @see java.awt.event.WindowListener#windowActivated(WindowEvent)
	 */
	@Override
	public void windowActivated(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	/**
	 * Method windowClosed.
	 * 
	 * @param e
	 *            WindowEvent
	 * @see java.awt.event.WindowListener#windowClosed(WindowEvent)
	 */
	@Override
	public void windowClosed(WindowEvent e) {
	}

	/**
	 * Method windowClosing.
	 * 
	 * @param e
	 *            WindowEvent
	 * @see java.awt.event.WindowListener#windowClosing(WindowEvent)
	 */
	@Override
	public void windowClosing(WindowEvent e) {
		System.exit(0);
	}

	/**
	 * Method windowDeactivated.
	 * 
	 * @param e
	 *            WindowEvent
	 * @see java.awt.event.WindowListener#windowDeactivated(WindowEvent)
	 */
	@Override
	public void windowDeactivated(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	/**
	 * Method windowDeiconified.
	 * 
	 * @param e
	 *            WindowEvent
	 * @see java.awt.event.WindowListener#windowDeiconified(WindowEvent)
	 */
	@Override
	public void windowDeiconified(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	/**
	 * Method windowIconified.
	 * 
	 * @param e
	 *            WindowEvent
	 * @see java.awt.event.WindowListener#windowIconified(WindowEvent)
	 */
	@Override
	public void windowIconified(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	/**
	 * Method windowOpened.
	 * 
	 * @param e
	 *            WindowEvent
	 * @see java.awt.event.WindowListener#windowOpened(WindowEvent)
	 */
	@Override
	public void windowOpened(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	/**
	 * Method mouseClicked.
	 * 
	 * @param e
	 *            MouseEvent
	 * @see java.awt.event.MouseListener#mouseClicked(MouseEvent)
	 */
	@Override
	public void mouseClicked(MouseEvent e) {
		try
		{
			AdminFrame frame = AdminFrame.getInstance();
			frame.pack();
			frame.setVisible(true);
			frame.moveToTop(this);
		}
		catch(Exception ex)
		{
			ex.printStackTrace();	
			System.out.println("admin frame openning error!");	
		}
	}

	/**
	 * Method mousePressed.
	 * 
	 * @param e
	 *            MouseEvent
	 * @see java.awt.event.MouseListener#mousePressed(MouseEvent)
	 */
	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	/**
	 * Method mouseReleased.
	 * 
	 * @param e
	 *            MouseEvent
	 * @see java.awt.event.MouseListener#mouseReleased(MouseEvent)
	 */
	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	/**
	 * Method mouseEntered.
	 * 
	 * @param e
	 *            MouseEvent
	 * @see java.awt.event.MouseListener#mouseEntered(MouseEvent)
	 */
	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	/**
	 * Method mouseExited.
	 * 
	 * @param e
	 *            MouseEvent
	 * @see java.awt.event.MouseListener#mouseExited(MouseEvent)
	 */
	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void updateView(Object obj, Object additionalInformation) {
		final String classMethod = className + ".updateView(Obj, additionalInfo)";
		LoggingUtils.logStart(classMethod + " with parameters of type (" 
				+ ((obj != null) ? obj.getClass().getName() : "null") + ", "
				+ ((additionalInformation != null) ? additionalInformation.getClass().getName() : "") + ")");		
		try {
			LoggingUtils.logStart("invokeLater() of Runnable to configure and repaint PlayerCanvas in " + classMethod);
			SwingUtilities.invokeLater(new Runnable() {

				@Override
				public void run() {
					LoggingUtils.logStart("run() of Runnable to configure and repaint PlayerCanvas in " + classMethod);
					Preference preference = Preference.getInstance();
					PlayerCanvas.this.setSize(new Dimension(PlayerUtilities.scale(preference.getPlayerWidth(), preference.getScale()), PlayerUtilities.scale(
							preference.getPlayerHeight(), preference.getScale())));
					PlayerCanvas.this.setLocation(0, 0);
					PlayerCanvas.this.validate();
					PlayerCanvas.this.repaint();
					LoggingUtils.logEnd("run() of Runnable to configure and repaint PlayerCanvas in " + classMethod);
				}
			});
		} catch (Exception e) {
			LoggingUtils.log(Level.SEVERE, "Cannot update PlayerCanvas in " + classMethod + e.getMessage(), e);
			e.printStackTrace();
		} 
		LoggingUtils.logEnd(classMethod); 
	}
}
